<?php
class GoodsController extends Controller{
	protected $obj = null;
	private $_urlPrefix = 'index.php?p=admin&c=goods';
	
	public function __construct() {
		parent::__construct();
		$this -> obj = new GoodsModel();
	}
	
	public function insertAction() {
		$upload = new upload;
		if($fileName = $upload->up($_FILES['pic'])){
			$_POST['add_by'] = $_SESSION['uid'];
			$_POST['add_time'] = $_SERVER['REQUEST_TIME'];
			$_POST['goods_img'] = $fileName;
			
			$result = $this -> obj -> autoExecute($_POST);
			
			if($result) {
				$this -> jump($this -> _urlPrefix . '&a=list', '添加成功');
			} else {
				$this -> jump($this -> _urlPrefix . '&a=add', '添加失败');
			}
		} else {
			$this -> jump($this -> _urlPrefix . '&a=add', '上传图片失败');
		}
	}
	
	public function addAction() {
		$cateObj = new CategoryModel();
		$cates = $cateObj -> getCategory();
		
		include __VIEW__ . PLATFORM . DS . 'goodsadd.html';
	}
	
	public function listAction() {
		$goods = $this -> obj -> getGoods();
		$i = 1;
		
		include __VIEW__ . PLATFORM . DS . 'goodslist.html';
	}
	
	public function modifyAction() {
		$goodsId = isset($_GET['goods_id']) ? $_GET['goods_id'] + 0 : 0;

		if(!$goodsId) {
			$this -> jump($this -> _urlPrefix . '&a=list', '未知错误');
		}
		
		$cateObj = new CategoryModel();
		$cates = $cateObj -> getCategory();
		
		$goodsObj = new GoodsModel();
		$goods = $goodsObj -> getGoodsRow($goodsId);
		
		include __VIEW__ . PLATFORM . DS . 'goodsmodify.html';
	}
	
	public function delAction() {
		$goodsId = isset($_GET['goods_id']) ? $_GET['goods_id'] + 0 : 0;

		if(!$goodsId) {
			$this -> jump($this -> _urlPrefix . '&a=list', '未知错误');
		}
		
		$goodsObj = new GoodsModel();
		$result = $goodsObj -> delGoods($goodsId);
		
		if($result) {
			$this -> jump($this -> _urlPrefix . '&a=list', '删除成功');
		} else {
			$this -> jump($this -> _urlPrefix . '&a=list', '删除失败');
		}
	}
	
	public function updateAction() {
		$upload = new upload;
		if($fileName = $upload->up($_FILES['pic'])){
			$_POST['add_by'] = $_SESSION['uid'];
			$_POST['add_time'] = $_SERVER['REQUEST_TIME'];
			$_POST['goods_img'] = $fileName;
			$goodsId = isset($_POST['goods_id']) ? $_POST['goods_id'] + 0 : 0;
			unset($_POST['goods_id']);
			var_dump($_POST);
			$result = $this -> obj -> autoExecute($_POST, "where goods_id = {$goodsId}", 'update');
			
			if($result) {
				$this -> jump($this -> _urlPrefix . '&a=list', '修改成功');
			} else {
				$this -> jump($this -> _urlPrefix . '&a=list', '修改失败');
			}
		} else {
			$this -> jump($this -> _urlPrefix . '&a=add', '上传图片失败');
		}
	}
}
?>
